#!/usr/bin python3
# -*- coding:UTF-8 -*-
# Author: nigo
import plotly.graph_objects as go
import pandas as pd
import requests

def get_geojson(code):
    """获取行政区划的geojson
    code:行政区划代码
    return:geojson"""
    url = 'https://geo.datav.aliyun.com/areas_v2/bound/%s_full.json' % code
    response = requests.get(url)
    geojson = response.json()
    return geojson

if __name__ == "__main__":
    # 数据 （复制随书附件后运行）
    df = pd.read_clipboard(delimiter='\s+',thousands=',')
    # 获取四川省geojson
    country = get_geojson('510000')
    # 轨迹
    map = go.Choropleth(geojson=country, locations=df['行政代码'], z=df['GDP'],
                  featureidkey = "properties.adcode",text = df['地区'],
                  colorscale = 'Reds',colorbar_title='GDP(亿)')
    # 画布
    fig = go.Figure(map)
    fig.update_geos(fitbounds="locations", visible=False)
    # 更新样式
    fig.update_layout(title='四川省2020年各市GDP')
    # 显示画布
    fig.show()
